package com.guohu.shadow;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RadialGradient;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.view.ViewParent;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class ShadowView extends ViewGroup implements ViewParent, ViewManager {
    public ShadowView(Context context) {
        super(context);
        super.setWillNotDraw(false);
        createPaint();

//        initCanvas();
    }

    public ShadowView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        super.setWillNotDraw(false);
        createPaint();

//        initCanvas();

    }

    public ShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        super.setWillNotDraw(false);
        createPaint();
//        initCanvas();

    }

    public ShadowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        super.setWillNotDraw(false);
        createPaint();

//        initCanvas();

    }

    float top,left,bottom,right;

    int childTop,childLeft,childBottom,childRight;

    int shadowHeight=10;

    int centerX,centerY,radius;

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
        final int childCount=getChildCount();
        if (childCount>1){
            throw new RuntimeException("Child count can not >1");
        }

        View child=getChildAt(0);

         centerX= (int) (right/2);
         centerY= (int) (bottom/2);

        //         int he= (int) (bottom*bottom+right*right);
//         radius= (int) (bottom<right?right:bottom);
//        radius= child.getMeasuredHeight();
        radius= (int) right;
        childTop= (int) (shadowHeight);
        childLeft= (int) (shadowHeight);
        childBottom= (int) (child.getMeasuredHeight()+shadowHeight);
        childRight= (int) (child.getMeasuredWidth()+shadowHeight);

        child.layout(childLeft,childTop,childRight,childBottom);

    }

    /**
     * 要求所有的孩子测量自己的大小，然后根据这些孩子的大小完成自己的尺寸测量
     */
    @SuppressLint("NewApi") @Override
    protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) {

        //获得此ViewGroup上级容器为其推荐的宽和高，以及计算模式
        int widthMode = MeasureSpec. getMode(widthMeasureSpec);
        int heightMode = MeasureSpec. getMode(heightMeasureSpec);
        int sizeWidth = MeasureSpec. getSize(widthMeasureSpec);
        int sizeHeight = MeasureSpec. getSize(heightMeasureSpec);
        int layoutWidth = 0;
        int layoutHeight = 0;
        // 计算出所有的childView的宽和高
        measureChildren(widthMeasureSpec, heightMeasureSpec);

        int cWidth = 0;
        int cHeight = 0;
        int count = getChildCount();

        if(widthMode == MeasureSpec. EXACTLY){
            //如果布局容器的宽度模式是确定的（具体的size或者match_parent），直接使用父窗体建议的宽度
            layoutWidth = sizeWidth;
        } else{
            //如果是未指定或者wrap_content，我们都按照包裹内容做，宽度方向上只需要拿到所有子控件中宽度做大的作为布局宽度
            for ( int i = 0; i < count; i++)  {
                View child = getChildAt(i);
                cWidth = child.getMeasuredWidth();
                //获取子控件最大宽度
                layoutWidth = cWidth > layoutWidth ? cWidth : layoutWidth;
            }
        }
        //高度很宽度处理思想一样
        if(heightMode == MeasureSpec. EXACTLY){
            layoutHeight = sizeHeight;
        } else{
            for ( int i = 0; i < count; i++)  {
                View child = getChildAt(i);
                cHeight = child.getMeasuredHeight();
                layoutHeight = cHeight > layoutHeight ? cHeight : layoutHeight;
            }
        }

        // 测量并保存layout的宽高
        layoutWidth=layoutWidth+shadowHeight*2;
        layoutHeight=layoutHeight+shadowHeight*2;

        top=0;
        left=0;
        right=layoutWidth;
        bottom=layoutHeight;

        setMeasuredDimension(layoutWidth, layoutHeight);

    }

    private Paint paint;
    private Shader mSweepGradient = null;

    void createPaint(){
        paint=new Paint();
//        mSweepGradient=new SweepGradient(0, getMeasuredHeight(), new int[] {0xffcd5b, Color.GRAY}, null);
        initPaint();
    }

    void initPaint(){
        paint.setStyle(Paint.Style.FILL);
        paint.setColor( Color.GRAY );
        paint.setStrokeWidth(1);//设置线宽
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawShadow(canvas);
    }

    private void drawShadow(Canvas canvas){
        /**
         *
        canvas.drawLine(left,top,right,top,paint);
        float centerX,centerY;
        centerX=(left+right)/2;
        centerY=(top+bottom)/2;
        canvas.drawPoint(centerX,centerY,paint);
         */
        Log.e("----drawShadow----","-----------");
        Log.e("----centerX----","-----------"+centerX);

        Log.e("----centerY----","-----------"+centerY);

        Log.e("----left----","-----------"+left);

        Log.e("----top----","-----------"+top);
        Log.e("----right----","-----------"+right);

        Log.e("----bottom----","-----------"+bottom);

//        mSweepGradient=new SweepGradient(centerX, centerY, new int[] {Color.GREEN, Color.GRAY}, null);
//        mSweepGradient=new LinearGradient(left,top,right,bottom,new int[]{Color.WHITE,Color.GRAY},null, Shader.TileMode.MIRROR );
        mSweepGradient=new RadialGradient(centerX,centerY,radius,new int[]{0xff808080,Color.WHITE},null,Shader.TileMode.CLAMP);

        paint.setShader(mSweepGradient);
        Path bottomPath=getBottomPath();
        canvas.drawPath(bottomPath,paint);
        canvas.drawPath(getLeftPath(),paint);
        canvas.drawPath(getTopPath(),paint);
        canvas.drawPath(getRightPath(),paint);

    }

    private Path getBottomPath(){
        Path path=new Path();
        path.moveTo(left,bottom);
        path.lineTo(childLeft,childBottom);
        path.lineTo(childRight,childBottom);
        path.lineTo(right,bottom);
        path.close();
        return path;
    }

    private Path getTopPath(){
        Path path=new Path();
        path.moveTo(left,top);
        path.lineTo(childLeft,childTop);
        path.lineTo(childRight,childTop);
        path.lineTo(right,top);
        path.close();
        return path;
    }

    private Path getLeftPath(){
        Path path=new Path();
        path.moveTo(left,top);
        path.lineTo(childLeft,childTop);
        path.lineTo(childLeft,childBottom);
        path.lineTo(left,bottom);
        path.close();
        return path;
    }

    private Path getRightPath(){
        Path path=new Path();
        path.moveTo(right,top);
        path.lineTo(childRight,childTop);
        path.lineTo(childRight,childBottom);
        path.lineTo(right,bottom);
        path.close();
        return path;
    }

}
